package com.komodo.island.gdx.game.komodophoto;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.komodo.island.gdx.AnimatedSprite;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.BubbleNotif;
import com.komodo.island.gdx.Constants;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.dialog.LoseDialog;
import com.komodo.island.gdx.dialog.WinDialog;
import com.komodo.island.gdx.game.Time;
import com.komodo.island.gdx.screen.GameScreen;
import com.komodo.island.gdx.screen.Screen;

public class KomodoPhoto extends GameScreen {

	public static final int HIT = 0;
	public static final int COMBO = 1;
	public static final int SUPER = 2;
	public static final int DOUBLE = 3;
	public static final int AWESOME = 3;
	public static final int MISS = 5;

	private int worldHeight;
	private Time time;
	private Random randomizer;

	private int secondLimit = 60;
	private int nKomodoCaptured = 0;
	private int nBoarCaptured = 0;

	private WildKomodo komodoSprites[];
	private WildBoar boarSprites[];

	public TextureRegion[][] spriteRegion;
	public TextureRegion bingkai;
	public TextureRegion bgphoto;

	public TextureRegion blitz;
	public TextureRegion combos[];

	private double startWaveTime;
	private double currWaveTime;

	private float rand;
	private float stateTime;
	private int count;
	private int countBoar;
	private double frameSecond;
	private int moveObjectCurSecond = -1;

	private List<BubbleNotif> bubbleNotifs;
	private BubbleNotif komodoCombo;
	// private BubbleNotif boarCombo;

	public Texture komodoPhotoTexture;
	private TextureRegion[] komodoRegion;

	private Sound click;

	@Override
	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);
		previousScreen = komodoIsland.scientistScreen;

		if (bubbleNotifs == null) {
			bubbleNotifs = new ArrayList<BubbleNotif>();
			for (int i = 0; i < 10; i++) {
				bubbleNotifs.add(new BubbleNotif(batch, null, 0, 0, 60, 64));
			}
			komodoCombo = new BubbleNotif(batch, null, 480 - 200, 160 , 60, 64);
			// boarCombo = new BubbleNotif(batch, combos[MISS], 0, 320 -
			// 64,60,64);
		}

	}

	@Override
	public void onGameRunning() {
		currWaveTime = System.currentTimeMillis();
		time.runTime();
		// handle the logic

		if (moveObjectCurSecond != (int) (time.getSecond() * 10)) {
			moveObjectCurSecond = (int) (time.getSecond() * 10);

			for (int i = 0; i < 5; i++) {
				if (komodoSprites[i].isAvailable) {
					komodoSprites[i].move(stateTime);
				} else {
					rand = randomizer.nextFloat();
					// random encounter
					if ((((rand) > 0.8) && ((rand) < 0.809))
							|| ((((rand) > 0.5) && ((rand) < 0.501)))) {
						komodoSprites[i].resetToShow(randomizer);
					}
				}

				if (boarSprites[i].isAvailable) {
					boarSprites[i].move(stateTime);
				} else {
					rand = randomizer.nextFloat();
					if ((((rand) > 0.7) && ((rand) < 0.709))) {
						boarSprites[i].resetToShow(randomizer);
					}
				}
			}

		}

		// handle the drawing
		batch.begin();
		batch.draw(bgphoto, 0, 0);
		for (int i = 0; i < 5; i++) {
			if (komodoSprites[i].isAvailable) {
				komodoSprites[i].draw(batch);
			}

			if (boarSprites[i].isAvailable) {
				boarSprites[i].draw(batch);
			}
		}
		batch.draw(bingkai, 0, 0);
		batch.end();

//		 komodoCombo.draw();
//		 boarCombo.draw();

		Art.drawString("Time : " + Art.decimal2.format(time.getSecond()), 0, -2);
		Art.drawString("Score : " + score, 0, 320 - 30);

		for (BubbleNotif notif : bubbleNotifs) {
			notif.draw();

		}

		stateTime += Gdx.graphics.getDeltaTime();
		frameSecond = time.getSecond();

		// handling input
		if (Gdx.input.justTouched()) {
			if (Persistence.sound)
				click.play(1);

			count = 0;
			countBoar = 0;
			for (int i = 0; i < 5; i++) {
				BubbleNotif notif;
				if (komodoSprites[i].isAvailable
						&& (komodoSprites[i].getX()
								 >= 30)
						&& (komodoSprites[i].getX()+ komodoSprites[i].getWidth() <= 380)) {
					komodoSprites[i].isAvailable = false;
					notif = bubbleNotifs.get(i);
					// notif.notif = "HIT";
					// notif.notif = "ARGH";
					// if (notif.notif == null){
					notif.notif = combos[HIT];
					// }
//					if (komodoSprites[i].direction == 1)// left
						notif.x = komodoSprites[i].getX()
								+ komodoSprites[i].getWidth()/2-30 ;
//					else
//						notif.x = komodoSprites[i].getX();
					notif.y = komodoSprites[i].getY()+komodoSprites[i].getHeight()/2-16;
					notif.show();
					count++;
				}
				if (boarSprites[i].isAvailable
						&& (boarSprites[i].getX()  >= 30)
						&& (boarSprites[i].getX()+ boarSprites[i].getWidth() <= 380)) {
					boarSprites[i].isAvailable = false;
					notif = bubbleNotifs.get(i + 4);
					// notif.notif = "ARGH";
					// if (notif.notif == null){
					notif.notif = combos[MISS];
					// }
//					if (boarSprites[i].direction == 1)// left
						notif.x = boarSprites[i].getX()
								+ boarSprites[i].getWidth()/2-30;
//					else
//						notif.x = boarSprites[i].getX();

						notif.y = boarSprites[i].getY()+boarSprites[i].getHeight()/2-16;
					notif.show();
					countBoar++;
				}
			}

			// handling scoring
			if (count >= 5) {
				score = score + count * 2 * (1000 );
				komodoCombo.notif = combos[AWESOME];
				komodoCombo.show();
			} else if (count >= 3) {
				score = score + count * 2 * (1000);
				komodoCombo.notif = combos[SUPER];
				komodoCombo.show();
			} else if (count >= 1 && count < 3) {
				score = score + count * (1000);
				komodoCombo.notif = combos[DOUBLE];
				komodoCombo.show();
			} else if (count == 0) {
				score -= 1000 + randomizer.nextInt(500);
			}

			if (countBoar >= 5) {
				score = score - countBoar * 2 * (800 );
			} else if (countBoar >= 3) {
				score = score - countBoar * 2 * (800 );
			} else if (countBoar >= 1 && countBoar < 3) {
				score = score - countBoar * (800 );
			} else if (countBoar == 0) {
				score += randomizer.nextInt(500);
			}
			nKomodoCaptured += count;
			nBoarCaptured += countBoar;
			count = 0;
			countBoar = 0;

//			for(int i =0;i<10;i++){
			batch.begin();
			batch.draw(blitz, 0, 0);
			batch.end();
//			}
			
		}

		if (score <= -10000){
			setGameToLose();
		}
		
		// Game over -> win or lose ?? game end condition
		if (time.getMinute() == 0 && (Math.ceil(time.getSecond()) == 0)) {

			if (nKomodoCaptured >= 30) {
				score += 20000;
			}
			if (nBoarCaptured <= 1) {
				score += 20000 - nBoarCaptured;
			}
			if (score >= 10000 && nKomodoCaptured >= 10) {
				setGameToWin();
			} else {
				setGameToLose();
			}
			// win jika score >= 10 ribu dan berhasil menangkap 10 foto komodo..
			// bonus bila komodo > 50
			// dan juga kalo ga tangkap2 boar
		}
	}

	@Override
	public void onGameWin() {
	}

	@Override
	public void onGameLose() {
	}

	@Override
	public void onGamePause() {

	}

	@Override
	public void onGameStart() {
		// initialize the variable
		// bestScore = Persistence.getBestScore(Persistence.PHOTO);
		nKomodoCaptured = 0;
		nBoarCaptured = 0;

		worldHeight = Gdx.graphics.getWidth();
		secondLimit = 60;
		score = 0;

		// Construct new object;
		if (time == null) {
			time = new Time(0, secondLimit);
		} else {
			time.setSecond(secondLimit);
		}

		if (randomizer == null)
			randomizer = new Random();

		frameSecond = time.getSecond();

		komodoSprites = new WildKomodo[5];
		boarSprites = new WildBoar[5];

		for (int i = 0; i < 5; i++) {
			komodoSprites[i] = new WildKomodo(komodoRegion);
			// komodoSprites[i].setSize(128, 64);
			boarSprites[i] = new WildBoar(spriteRegion);
			// boarSprites[i].setSize(64, 64);
		}
		startWaveTime = System.currentTimeMillis();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();

	}

	@Override
	public void cleanResScreen() {
		// TODO Auto-generated method stub
		super.cleanResScreen();
		try {
			komodoPhotoTexture.dispose();
			click.dispose();
			
		} catch (Exception e) {

		}
	}
	@Override
	public void exitScreen() {
		// TODO Auto-generated method stub
//		cleanResScreen();
		try {
			komodoPhotoTexture.dispose();
			click.dispose();
			komodoPhotoTexture= null;
			click = null;
		} catch (Exception e) {

		}
		super.exitScreen();
	}

	@Override
	protected void onGameLoading() {
		// TODO Auto-generated method stub
		if (click == null) {
			click = Gdx.audio.newSound(Gdx.files
					.internal("sfx/photo/click.wav"));
		}
		if (komodoIsland.photo == null) {
			komodoIsland.photo = Gdx.audio.newMusic(Gdx.files
					.internal("bgm/photo.mp3"));
			komodoIsland.photo.setLooping(true);
			komodoIsland.photo.setVolume(0.5f);
		}

		if (Persistence.sound) {
			try {
				if (!komodoIsland.photo.isPlaying()) {
					komodoIsland.photo.play();
				}
			} catch (Exception e) {

			}
		}

		if (komodoPhotoTexture == null) {
			komodoPhotoTexture = new Texture(Gdx.files.internal("photo1.png"));
			spriteRegion = new TextureRegion[7][2];
			for (int i = 0; i < 7; i++) {
				spriteRegion[i] = new TextureRegion[2];

				if (i < 6) {
					spriteRegion[i][0] = new TextureRegion(komodoPhotoTexture,
							128 * i, 0, 64, 64);
					spriteRegion[i][1] = new TextureRegion(komodoPhotoTexture,
							128 * i + 64, 0, 64, 64);
				} else if (i == 6) {
					spriteRegion[i][0] = new TextureRegion(komodoPhotoTexture,
							0, 64, 128, 64);
					spriteRegion[i][1] = new TextureRegion(komodoPhotoTexture,
							128, 64, 128, 64);
				}

			}
			komodoRegion = new TextureRegion[2];
			komodoRegion[0] = new TextureRegion(komodoPhotoTexture, 128 * 2,
					64, 128, 64);
			komodoRegion[1] = new TextureRegion(komodoPhotoTexture, 128 * 3,
					64, 128, 64);

			bingkai = new TextureRegion(komodoPhotoTexture, 128 * 6, 0, 480,
					320);
			bgphoto = new TextureRegion(komodoPhotoTexture, 128 * 6 + 480, 0,
					480, 320);

			blitz = new TextureRegion(komodoPhotoTexture, 0, 128, 480, 320);

			combos = new TextureRegion[6];
			combos[COMBO] = new TextureRegion(komodoPhotoTexture, 0, 128 + 320,
					60, 32);
			combos[HIT] = new TextureRegion(komodoPhotoTexture, 60, 128 + 320,
					60, 32);
			combos[DOUBLE] = new TextureRegion(komodoPhotoTexture, 120,
					128 + 320, 60, 32);
			combos[SUPER] = new TextureRegion(komodoPhotoTexture, 0,
					128 + 320 + 32, 60, 32);
			combos[AWESOME] = new TextureRegion(komodoPhotoTexture, 60,
					128 + 320 + 32, 60, 32);
			combos[MISS] = new TextureRegion(komodoPhotoTexture, 120,
					128 + 320 + 32, 60, 32);

			if (winDialog == null)
				this.winDialog = new PhotoWinDialog(Constants.GAME_WIDTH,
						Constants.GAME_HEIGHT, true, this,
						komodoIsland.dialogTexture,komodoIsland);
			if (loseDialog == null)
				this.loseDialog = new PhotoLoseDialog(Constants.GAME_WIDTH,
						Constants.GAME_HEIGHT, true, this,
						komodoIsland.dialogTexture,komodoIsland);
		}

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		try {
			if (Persistence.sound) {
				if (komodoIsland.photo.isPlaying()) {
					komodoIsland.photo.stop();
				}
			} else {

			}
		} catch (Exception e) {

		}
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		try {
			if (Persistence.sound) {
				if (!komodoIsland.photo.isPlaying()) {
					komodoIsland.photo.play();
				}
			} else {

			}
		} catch (Exception e) {

		}
	}

	private class WildKomodo extends AnimatedSprite {

		public final static int RIGHT = 0;
		public final static int LEFT = 1;

		public int speed;
		public int direction = RIGHT;
		public boolean isAvailable = false;// kalo true bisa digambar, bisa di
											// show

		public WildKomodo(TextureRegion[] frames) {
			super(frames);

		}

		public void move(float stateTime) {
			if (isAvailable) {
				if (direction == RIGHT) {
					setScale(1, 1);
					nextKey(stateTime);
					setPosition(getX() + speed + 10, getY());
					if (getX() >= 370) {
						isAvailable = false;

					}
				} else if (direction == LEFT) {
					setScale(-1, 1);
					nextKey(stateTime);
					setPosition(getX() - speed - 10, getY());

					if (getX() + getWidth() <= 34) {
						isAvailable = false;

					}
				}
			}

		}

		public void resetToShow(Random random) {
			int rand = random.nextInt(2);
			speed = random.nextInt(8);
			while (speed < 4) {
				speed = random.nextInt(11);
			}

			int y = random.nextInt(242);
			while (y + getHeight() >= 242) {
				y = random.nextInt(242);
			}

			if (rand == RIGHT) {
				direction = LEFT;
				setPosition(370 + 128, y);
			} else {
				direction = RIGHT;
				setPosition(34 - 128, y);
			}

			isAvailable = true;

		}

	}

	private class WildBoar extends AnimatedSprite {

		public final static int RIGHT = 0;
		public final static int LEFT = 1;

		public int speed;
		public int direction = RIGHT;
		public boolean isAvailable = false;// kalo true bisa digambar, bisa di
											// show
		public Animation spriteAnim[];

		public WildBoar(TextureRegion[][] frames) {
			super(frames[0]);
			spriteAnim = new Animation[7];
			spriteAnim[0] = animation;
			for (int i = 1; i < 7; i++) {
				spriteAnim[i] = new Animation(0.25f, frames[i]);
			}

			// TODO Auto-generated constructor stub
		}

		public void move(float stateTime) {
			if (isAvailable) {
				if (direction == RIGHT) {
					setScale(1, 1);
					nextKey(stateTime);
					setPosition(getX() + speed, getY());
					if (getX() >= 370) {
						randomAnim();
						isAvailable = false;

					}
				} else if (direction == LEFT) {
					setScale(-1, 1);
					nextKey(stateTime);
					setPosition(getX() - speed, getY());
					if (getX() + getWidth() <= 34) {
						randomAnim();
						isAvailable = false;

					}
				}
			}

		}

		public void resetToShow(Random random) {
			int rand = random.nextInt(2);
			speed = random.nextInt(10) + 15;
			// while (speed < 10) {
			// speed = random.nextInt(20);
			// }

			int y = random.nextInt(242);
			while (y + getHeight() >= 242) {
				y = random.nextInt(242);
			}

			if (rand == RIGHT) {
				direction = LEFT;
				setPosition(370 + 128, y);
			} else {
				direction = RIGHT;
				setPosition(34 - 128, y);
			}

			isAvailable = true;

		}

		public void randomAnim() {
			int i = randomizer.nextInt(7);
			setNewAnimation(spriteAnim[i]);
		}

	}

	private class PhotoWinDialog extends WinDialog {

		public Image finish;

		public PhotoWinDialog(float width, float height, boolean stretch,
				Screen screen, Texture texture,KomodoIsland komodoIsland) {
			super(width, height, stretch, screen, texture,komodoIsland);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void init() {
			// TODO Auto-generated method stub
			super.init();
			finish = new Image("finish", new TextureRegion(
					screen.komodoIsland.additionTexture, 0, 760, 96, 96));
		}

		@Override
		public void addElements() {
			// TODO Auto-generated method stub
			super.addElements();
			addActor(finish);
		}

		@Override
		public void setThePosition() {
			// TODO Auto-generated method stub
			super.setThePosition();
			finish.x = siKomo.x;
			finish.y = siKomo.y;

		}

	}

	private class PhotoLoseDialog extends LoseDialog {

		public Image finish;

		public PhotoLoseDialog(float width, float height, boolean stretch,
				Screen screen, Texture texture,KomodoIsland komodoIsland) {
			super(width, height, stretch, screen, texture,komodoIsland);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void init() {
			// TODO Auto-generated method stub
			super.init();
			finish = new Image("finish", new TextureRegion(
					screen.komodoIsland.additionTexture, 96, 760, 96, 96));
		}

		@Override
		public void addElements() {
			// TODO Auto-generated method stub
			super.addElements();
			addActor(finish);
		}

		@Override
		public void setThePosition() {
			// TODO Auto-generated method stub
			super.setThePosition();
			finish.x = siKomo.x;
			finish.y = siKomo.y;

		}

	}

}
